package com.cw.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cw.elasticsearch.service.ElasticsearchService;
import com.cw.elasticsearch.service.impl.ElasticsearchServiceImpl;

/**
 *
 * @author chenw
 * @date 2020/12/20
 */
@Configuration
public class ElasticSearchConfig {
    @Value("${elasticsearch.host:127.0.0.1}")
    private String elasticsearchHost;
    @Value("${elasticsearch.port:9200}")
    private Integer elasticsearchPort;
    @Bean
    public ElasticsearchService getElasticsearchService(){
        return new ElasticsearchServiceImpl();
    }
    /**
     * 单机
     *
     * @return
     */
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                new HttpHost(elasticsearchHost, elasticsearchPort, "http")));
        return client;
    }

    // 集群
    // @Bean
    // public RestHighLevelClient restHighLevelClient() {
    //     RestHighLevelClient client = new RestHighLevelClient(
    //         RestClient.builder(
    //             new HttpHost("127.0.0.1", 9200, "http"),
    //             new HttpHost("127.0.0.1",9201,"http")));
    //     return client;
    // }

}
